找传奇、传世资源到传世资源站!

delphi 串行通信 示例源码(连接串口、断开串口)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

from clipboard
unit PMain;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;const WM_COMMNOTIFY=WM_USER 12;type TMainForm = class(TForm) ComboBox1: TComboBox; Button1: TButton; Label1: TLabel; Button2: TButton; procedure FormCreate(Sender: TObject); private { Private declarations } Procedure CommInitialize; Procedure MsgCommProcess(var Message:TMessage);Message WM_COMMNOTIFY; public { Public declarations } end; TComm= class(TThread) protected Procedure Execute;override; end;var MainForm: TMainForm; hcom,Post_Event:Thandle; lpol:Poverlapped;implementation{$R *.DFM}{ TComm }procedure TComm.Execute;var dwEvtMask:Dword; Wait:Boolean;begin fillchar(lpol,sizeof(lpol),0); While True do begin dwEvtMask:=0; Wait:=WaitCommEvent(hcom,dwEvtMask,lpol); if Wait then begin WaitForSingleObject(Post_Event,infinite); PostMessage(MainForm.Handle,WM_COMMNOTIFY,0,0); end; end; inherited;end;{ TMainForm }procedure TMainForm.CommInitialize;var lpdcb:Tdcb;begin hcom:=CreateFile('Com2',generic_read or generic_write,0,nil,open_existing,file_attribute_normal or file_flag_overlapped,0); if hcom=invalid_handle_value then begin ShowMessage('端口初始化成功'); end else begin SetUpComm(hcom,4096,4096); end; GetCommState(hcom,lpdcb); lpdcb.BaudRate:=2400; lpdcb.StopBits:=1; lpdcb.ByteSize:=8; lpdcb.Parity:=EvenParity; SetCommState(hcom,lpdcb); SetCommMask(hcom,ev_rxchar);end;procedure TMainForm.MsgCommProcess(var Message: TMessage);var Clear:Boolean; Coms:TComstat; cbNum,ReadNumber,lpErrors:cardinal; Read_Buffer:array[1..100] of char;begin Clear:=ClearCommError(hcom,lpErrors,@Coms); if Clear then begin cbNum:=Coms.cbInQue; ReadFile(hcom,Read_Buffer,cbNum,ReadNumber,lpol); ShowMessage(Read_Buffer); SetEvent(Post_Event); end;end;procedure TMainForm.FormCreate(Sender: TObject);begin CommInitialize; Post_Event:=CreateEvent(nil,true,true,nil); TComm.Create(False);end;end.

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复